
//import java.net.ServerSocket;
//import java.net.Socket;
import java.io.*;
import java.net.*;
import java.util.Date;
import java.util.StringTokenizer;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author vladi
 */
public class HttpRequest implements Runnable {

    final static String CRLF = "\r\n";
    Socket socket;
    public HttpRequest(Socket socket)
    {
        this.socket = socket;
    }
    public void run()  {
        try{
            this.Processamento();
        }
        catch(Exception e)
        {
            System.out.println(e.toString());
        }
        //socket.getOutputStream();
        //socket
        //this.socket
        //throw new UnsupportedOperationException("Not supported yet.");
    }

    public String tipo (String terminacao)
    {
        char[] temp = terminacao.toCharArray();
        int i;
        for(i = temp.length -1; i >= 0 ; i--)
        {
            if(temp[i] == '.')
            {
                break;
            }
        }
        String tipo = terminacao.substring(i+1);
        System.out.println(tipo);
        if(tipo.equals("htm") || tipo.equals("html"))
        {
            return "text/html";
        }
        if(tipo.equals("gif"))
        {
            return "image/gif";
        }
        if(tipo.equals("jpeg") || tipo.equals("jpg"))
        {
            return "image/jpeg";
        }
        return "application/octet-stream";
    }

    public void Processamento () throws Exception
    {

        BufferedReader dadosDeEntrada = new BufferedReader ( new InputStreamReader (socket.getInputStream()));
        String entrada = dadosDeEntrada.readLine();
        DataOutputStream saida = new DataOutputStream (socket.getOutputStream());
        String[] requisicao = entrada.split(" ");
        String nomeDoArquivo = null;
        String versao = null;
        Date modificado;

        FileInputStream file;
        int controle = 0;
        byte[] b = new byte[1024];
        System.out.println(entrada);

        if(requisicao[0].equals("GET"))
        {
            nomeDoArquivo = "." + requisicao[1];
            versao = requisicao[2];
            //System.out.println(nomeDoArquivo);
        }

        String temp = this.tipo(requisicao[1]);

        file = new FileInputStream(nomeDoArquivo);
        File informacoes = new File(nomeDoArquivo);
        modificado = new Date(informacoes.lastModified());
        String ultMod = modificado.toString();
        modificado = new Date(System.currentTimeMillis());
        String dataAtual = modificado.toString();
        
        //System.out.println(temp);

        while( entrada.length() != 0 )
        {
            entrada = dadosDeEntrada.readLine();
            System.out.println(entrada);
            
        }
        //System.out.println(ultMod);
        //System.out.println(dataAtual);
       
        saida.writeBytes(versao + " 200 OK"+ CRLF );
        saida.writeBytes("DATE: " + dataAtual + CRLF);
        saida.writeBytes("Last-Modified: " + ultMod + CRLF);
        saida.writeBytes("Content-Lenght: " + informacoes.length() + CRLF);
        saida.writeBytes("Content-Type: "+ temp + CRLF); //+ this.tipo(requisicao[1]));

        saida.writeBytes(CRLF);
        while((controle =(file.read(b))) != -1)
        {
            saida.write(b, 0, controle);
        }
        file.close();
        saida.close();
        dadosDeEntrada.close();
        socket.close();

    }

}
